import pygame as SDL
from pygame.locals import *
import os,sys
import pENGINE, pAI

class Paddle(pENGINE.Sprite):
    def __init__(self,name,x,y):
        pENGINE.Sprite.__init__(self,name,x,y)
        self.velocity = 0
        self.moveTo(x,y)
        
    def vUp(self):
        self.velocity += -8
        
    def vDown(self):
        self.velocity += 8
        
    def vStop(self):
        self.velocity = 0
        
    def update(self):
        self.y += self.velocity
        self.moveTo(self.x,self.y)


class Ball(pENGINE.Sprite):
    def __init__(self,name,x,y):
        pENGINE.Sprite.__init__(self,name,x,y)
        self.yVelocity = -4
        self.xVelocity = 7
        self.moveTo(x,y)
        
    def update(self):
        self.x += self.xVelocity
        self.y += self.yVelocity
        self.moveTo(self.x,self.y)
        

class Enemy(Paddle):
    def __init__(self,name,x,y):
        Paddle.__init__(self,name,x,y)
        self.state = pENGINE.StateMachine()
        self.state.push(pAI.Chase())
        
    def vUp(self):
        self.velocity = -5
        
    def vDown(self):
        self.velocity = 4
        
    def update(self,ball,player):
        self.state.run(ball=ball,paddle=self,player=player)
        

class MenuPaddle(Paddle):
    def vDown(self):
        self.velocity = 4
        
    def vUp(self):
        self.velocity = -5
        
    def update(self,ball):
        if self.x > 320:
            if 0 < ball.xVelocity:
                if ball.y > (self.y+20):
                    self.vDown()
                elif ball.y < (self.y+20):
                    self.vUp()
            else:
                self.vStop()
        else:
            if 0 > ball.xVelocity:
                if ball.y > (self.y+20):
                    self.vDown()
                elif ball.y < (self.y+20):
                        self.vUp()
            else:
                self.vStop()
        
        self.y += self.velocity
        self.moveTo(self.x,self.y)


class ScoreChart:
    def __init__(self,text,color,width):
        self.font = SDL.font.Font(None,36)
        self.color = color
        self.update(text,width)
        
    def update(self,text,width):
        self.text = self.font.render(text,1,self.color)
        self.textpos = self.text.get_rect(centerx=width/2)
        
    def draw(self,screen):
        screen.blit(self.text,self.textpos)

class Nebula:
    def __init__(self,name):
        self.image = pENGINE.Sprite(name,0,0)
        self.x = 0
        
    def draw(self,screen):
        if self.x > 0:
            self.image.moveTo(self.x-512,0)
            self.image.draw(screen)
            self.image.moveTo(self.x,0)
            self.image.draw(screen)
            self.image.moveTo(self.x+512,0)
            self.image.draw(screen)
        elif self.x == 0:
            self.image.moveTo(0,0)
            self.image.draw(screen)
            self.image.moveTo(512,0)
            self.image.draw(screen)
        
        self.x += 1
        
        if self.x == 512:
            self.x = 0


class Clouds:
    def __init__(self,name):
        self.image = pENGINE.Sprite(name,0,0)
        self.x = 0
        
    def draw(self,screen):
        if self.x < 0:
            self.image.moveTo(self.x+1024,0)
            self.image.draw(screen)
            self.image.moveTo(self.x,0)
            self.image.draw(screen)
            self.image.moveTo(self.x+512,0)
            self.image.draw(screen)
        elif self.x == 0:
            self.image.moveTo(0,0)
            self.image.draw(screen)
            self.image.moveTo(512,0)
            self.image.draw(screen)
        
        self.x -= 1
        
        if self.x == -512:
            self.x = 0
            





